package onem.soria.entity;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;

import java.nio.charset.StandardCharsets;

@Getter
public class MetaData {
    String name;
    String[] artists;
    String album;
    Integer bitrate;
    String format;

    private final JSONObject metaDataJson;
    public MetaData(byte[] data) {
        String metaData = new String(data, StandardCharsets.UTF_8);
        this.metaDataJson = JSON.parseObject(metaData);
        this.setData(metaDataJson);
    }

    private void setData(JSONObject json) {
        this.name = json.getString("musicName");
        JSONArray artists = json.getJSONArray("artist");
        String[] artistName = new String[artists.size()];
        for (int i = 0; i < artists.size(); i++) {
            artistName[i] = artists.getJSONArray(i).getString(0);
        }
        this.artists = artistName;
        this.album = json.getString("album");
        this.bitrate = json.getInteger("bitrate");
        this.format = json.getString("format");
    }
}